﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Text.RegularExpressions;
using Com.CodePlex.SPC3.SPbAM.Properties;

namespace Com.CodePlex.SPC3.SPbAM.Model {

	public interface IServerResourceContainer {
		IList<object> Children { get; }
	}

	public class ServerWorkspace : IServerResourceContainer, IDataErrorInfo {

		static readonly string[] ValidatedProperties = { 
			"Name", 
			"Filename"
		};

		public string Name { get; set; }

		private readonly List<object> children = new List<object>();

		public IList<object> Children {
			get { return children; }
		}

		public string Filename { get; set; }

		string IDataErrorInfo.Error { get { return null; } }

		string IDataErrorInfo.this[string propertyName] {
			get { return this.GetValidationError(propertyName); }
		}

		public bool IsValid {
			get {
				foreach (string property in ValidatedProperties) {
					if (GetValidationError(property) != null) {
						return false;
					}
				}
				return true;
			}
		}

		private string GetValidationError(string propertyName) {
			if (Array.IndexOf(ValidatedProperties, propertyName) < 0) {
				return null;
			}
			string error = null;
			switch (propertyName) {
				case "Name":
					error = this.ValidateName();
					break;
				case "Filename":
					error = this.ValidateFilename();
					break;
				default:
					Debug.Fail("Unexpected property being validated on ServerWorkspace: " + propertyName);
					break;
			}
			return error;
		}

		private string ValidateName() {
			if (IsStringMissing(this.Name)) {
				return Resources.ServerWorkspace_Error_MissingName;
			}
			return null;
		}

		private string ValidateFilename() {
			if (IsStringMissing(this.Filename)) {
				return Resources.ServerWorkspace_Error_MissingFilename;
			}
			return null;
		}

		private static bool IsStringMissing(string value) {
			return String.IsNullOrEmpty(value) || value.Trim() == String.Empty;
		}

		/*
		public event EventHandler<EventArgs> ServerGroupAdded;
		public event EventHandler<EventArgs> ServerAdded;

		public void AddServerGroup(ServerGroup item) {
			if (item == null) {
				throw new ArgumentNullException("item");
			}
			/*
			if (!_customers.Contains(customer)) {
				_customers.Add(customer);

				if (this.CustomerAdded != null)
					this.CustomerAdded(this, new CustomerAddedEventArgs(customer));
			}
			 * * /
		}

		public bool ContainsServerGroup(ServerGroup item) {
			if (item == null) {
				throw new ArgumentNullException("item");
			}
			//return _customers.Contains(customer);
			return false;
		}

		public List<object> GetRootItems() {
			return new List<object>();
		}
		 * */
	}
}